<?php

/**
 * 默认控制器
 */
class Controller_Admintypes extends Controller_Abstract
{
	protected $_backend = true;

	function actionHtmltemplate()
	{
		$typeModel = new Types();
		
		if(isset($_GET['ope'])){
			$whichType = $this->_context->get('whichType', null);
			if($_GET['ope'] == 'add'){
				$whichType = $this->_context->post('whichType', null);
				if($whichType){
					$attributes = array();
					$attributes['name'] = $this->_context->post('name', '');
					$attributes['name_en'] = $this->_context->post('name_en', '');
					$attributes['tablename'] = 'htmltemplates';
					if($whichType == 'color'){
						$attributes['color_hex'] = $this->_context->post('color_hex', '');
					}
					$attributes['columnname'] = 'type_' . $whichType;
					$typeModel->attributes = $attributes;
					$opeResult = $typeModel->save();
				}
			}elseif($_GET['ope'] == 'setdefault'){
				$id = $this->_context->get('id', null);
				if($id){
					$opeResult = $typeModel->setDefaultById($id);
				}
			}elseif($_GET['ope'] == 'delete'){
				$id = $this->_context->get('id', null);
				if($id){
					$opeResult = $typeModel->delete(array('id' => $id));
				}
			}
			if($opeResult){
				return $this->_redirect(url('admintypes/htmltemplate', array('whichType' => $whichType)));
			}
		}
		$whichType = $this->_context->get('whichType', null);
		
		$this->_view['whichType'] = isset($whichType)? $whichType : '';
		$this->_view['htmltemplateTypes'] = Types::model()->find('*', array('tablename' => 'htmltemplates'), null, null, array('groupcolumnname' => true));
		$this->_view['typeModel'] = $typeModel;
		$this->_viewname = '../admin/types/htmltamplate';
	}
	
	
	function actionJqueryplugin()
	{
		$typeModel = new Types();
		
		if(isset($_GET['ope'])){
			if($_GET['ope'] == 'add'){
				$attributes = array();
				$attributes['name'] = $this->_context->post('name', '');
				$attributes['name_en'] = $this->_context->post('name_en', '');
				$attributes['tablename'] = 'jqueryplugins';
				$attributes['columnname'] = 'type_jq';
				$typeModel->attributes = $attributes;
				$opeResult = $typeModel->save();
			}elseif($_GET['ope'] == 'delete'){
				$id = $this->_context->get('id', null);
				if($id){
					$opeResult = $typeModel->delete(array('id' => $id));
				}
			}elseif($_GET['ope'] == 'setdefault'){
				$id = $this->_context->get('id', null);
				if($id){
					$opeResult = $typeModel->setDefaultById($id);
				}
			}
			if($opeResult){
				return $this->_redirect(url('admintypes/jqueryplugin'));
			}
		}
		
		
		$this->_view['types'] = $typeModel->find('*', array('tablename' => 'jqueryplugins', 'columnname' => 'type_jq'));
		$this->_view['typeModel'] = $typeModel;
		$this->_viewname = '../admin/types/jqueryplugin';
	}
	
	function actionFont()
	{
		$typeModel = new Types();
		
		if(isset($_GET['ope'])){
			$whichType = $this->_context->get('whichType', null);
			if($_GET['ope'] == 'add'){
				$whichType = $this->_context->post('whichType', null);
				if($whichType){
					$attributes = array();
					$attributes['name'] = $this->_context->post('name', '');
					$attributes['name_en'] = $this->_context->post('name_en', '');
					$attributes['tablename'] = 'fonts';
					$attributes['columnname'] = 'type_' . $whichType;
					$typeModel->attributes = $attributes;
					$opeResult = $typeModel->save();
				}
			}elseif($_GET['ope'] == 'setdefault'){
				$id = $this->_context->get('id', null);
				if($id){
					$opeResult = $typeModel->setDefaultById($id);
				}
			}elseif($_GET['ope'] == 'delete'){
				$id = $this->_context->get('id', null);
				if($id){
					$opeResult = $typeModel->delete(array('id' => $id));
				}
			}
			if($opeResult){
				return $this->_redirect(url('admintypes/font', array('whichType' => $whichType)));
			}
		}
		$whichType = $this->_context->get('whichType', null);
		
		$this->_view['whichType'] = isset($whichType)? $whichType : '';
		$this->_view['fontTypes'] = Types::model()->find('*', array('tablename' => 'fonts'), null, null, array('groupcolumnname' => true));
		$this->_view['typeModel'] = $typeModel;
		
		$this->_viewname = '../admin/types/font';
	}
}

